package com.yndsoft.jc.ui.xiaoyao;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.yndsoft.jc.adapter.KeyValueAdapter;
import com.yndsoft.jc.R;
import com.yndsoft.jc.model.KeyValue;
import com.yndsoft.jc.utils.StringUtils;

import java.util.List;

/**通用纵向Item选项dialog
 * @author lemon
 * @use new ItemDialog(...).show();
 */
public class KeyValueDialog extends Dialog implements OnItemClickListener {
	//	private static final String TAG = "ItemDialog";

	private Context context;
	private List<KeyValue> items;
	private String title;
	private int requestCode;
	private KeyValueClickListener listener;
	private TextView tvItemDialogTitle;
	private ListView lvItemDialog;
	private KeyValueAdapter adapter;

	public interface KeyValueClickListener {
		/**点击item事件的回调方法
		 * @param requestCode 传入的用于区分某种情况下的showDialog
		 * @param position
		 * @param keyValue
		 */
		void keyValueClick(int requestCode, int position, KeyValue keyValue);
	}

	/**
	 * 带监听器参数的构造函数
	 */
	public KeyValueDialog(Context context, List<KeyValue> items, int requestCode, KeyValueClickListener listener) {
		this(context, items, null, requestCode, listener);
	}
	public KeyValueDialog(Context context, List<KeyValue> items, String title, int requestCode, KeyValueClickListener listener) {
		super(context, R.style.MyDialog);
		this.context = context;
		this.items = items;
		this.title = title;
		this.requestCode = requestCode;
		this.listener = listener;  
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {  
		super.onCreate(savedInstanceState);  
		setContentView(R.layout.item_dialog); 
		setCanceledOnTouchOutside(true);

		tvItemDialogTitle = (TextView) findViewById(R.id.tvItemDialogTitle);
		lvItemDialog = (ListView) findViewById(R.id.lvItemDialog);

		tvItemDialogTitle.setVisibility(StringUtils.isNotEmpty(title, true) ? View.VISIBLE : View.GONE);
		tvItemDialogTitle.setText("" + StringUtils.getCurrentString());

		adapter = new KeyValueAdapter(context, items);
		lvItemDialog.setAdapter(adapter);
		lvItemDialog.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		if (listener != null) {
			//listener.onDialogItemClick2(requestCode, position, items.get(position));
			listener.keyValueClick(requestCode, position, (KeyValue) adapter.getItem(position));
		}
		dismiss();  
	}
}

